package com.mimose.cosy.proxy;

import com.mimose.cosy.instance.annotations.Singleton;
import com.mimose.cosy.instance.Instances;
import com.mimose.cosy.lang.Objs;
import com.mimose.cosy.proxy.enums.ProxyType;

/**
 * @author mimose
 * @description proxy factory
 * @date 2021/4/24
 */
@Singleton
public final class Proxies {

    private Proxies() {}

    public static Proxies getInstance() {
        return ProxiesHolder.INSTANCE;
    }

    /**
     * get proxy type
     * @param object
     * @return
     */
    public ProxyType type(Object object) {
        if(Objs.isNull(object)) {
            return ProxyType.NONE;
        }
        final Class clazz = object.getClass();
        if(clazz.isInterface() || java.lang.reflect.Proxy.isProxyClass(clazz)) {
            return ProxyType.DYNAMIC;
        }
        return ProxyType.CGLIB;
    }

    /**
     * Holder
     */
    private static class ProxiesHolder {
        private static final Proxies INSTANCE = Instances.decide(Proxies.class);

    }
}
